package com.west_traveler.recyclerview_adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.west_traveler.entity.entity_ranking_page.RankingData;
import com.west_traveler.entity.entity_ranking_page.RankingHeadList;
import com.west_traveler.ximalayafm.R;
import com.west_traveler.ximalayafm.WebViewActivity;

import java.util.List;

/**发现榜单界面适配器
 * created by Wang_JunJie at 2016/12/5
 */
public class RankingFragmentRecyclerViewAdapter extends RecyclerView.Adapter{
    private Context context;
    private List<RankingData> datas;
    private RankingHeadList rankingHead;

    private final int TYPE_HEAD=-1;
    private final int TYPE_ITEM_ONE=0;
    private final int TYPE_ITEM_TWO=1;

    public RankingFragmentRecyclerViewAdapter(Context context, List<RankingData> datas, RankingHeadList rankingHead) {
        this.context = context;
        this.datas = datas;
        this.rankingHead = rankingHead;
    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
            return TYPE_HEAD;
            case 1:
                return TYPE_ITEM_ONE;
            case 2:
                return TYPE_ITEM_TWO;
        }
        return -2;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.i("调试","RankingFragmentRecyclerViewAdapter onCreateViewHolde");
        View view=null;
        switch (viewType) {
            case TYPE_HEAD:
                 view = LayoutInflater.from(context).inflate(R.layout.fragment_ranking_head_item, parent, false);
                HeadViewHolder headViewHolder = new HeadViewHolder(view);
                return headViewHolder;
            case TYPE_ITEM_ONE:
                 view = LayoutInflater.from(context).inflate(R.layout.fragment_ranking_item, parent, false);
                ItemViewHolder itemViewHolderOne =  new ItemViewHolder(view);
                return itemViewHolderOne;
            case TYPE_ITEM_TWO:
                view = LayoutInflater.from(context).inflate(R.layout.fragment_ranking_item, parent, false);
                ItemViewHolder itemViewHolderTwo = new ItemViewHolder(view);
                return itemViewHolderTwo;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE_HEAD:
                if(holder instanceof HeadViewHolder){
                    Picasso.with(context).load(rankingHead.getPic()).into(((HeadViewHolder) holder).iv_head);
                }
            break;
            case TYPE_ITEM_ONE:
                if(holder instanceof ItemViewHolder) {
                    ((ItemViewHolder) holder).tvItemTotalTitle.setText(datas.get(TYPE_ITEM_ONE).getTitle());
                   //绑定子recyclerview的适配器
                    ((ItemViewHolder) holder).recyclerViewSecond.setAdapter(new RankingFrgRecyclerSecondAdapter(context,datas.get(TYPE_ITEM_ONE)));

                }
                break;
            case TYPE_ITEM_TWO:
                if(holder instanceof ItemViewHolder) {
                    ((ItemViewHolder) holder).tvItemTotalTitle.setText(datas.get(TYPE_ITEM_TWO).getTitle());
                    ((ItemViewHolder) holder).recyclerViewSecond.setAdapter(new RankingFrgRecyclerSecondAdapter(context,datas.get(TYPE_ITEM_TWO)));

                }

                break;
        }

    }

    @Override
    public int getItemCount() {
        return 3;
    }

    //头部ViewHolder
    public class HeadViewHolder extends RecyclerView.ViewHolder{

        private final ImageView iv_head;


        public HeadViewHolder(View itemView) {
            super(itemView);
            iv_head = ((ImageView) itemView.findViewById(R.id.iv_find_frg_ranking_head));
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, WebViewActivity.class);
                    String url = rankingHead.getUrl();
                    String title = rankingHead.getShortTitle();
                    intent.putExtra("url",url);
                    intent.putExtra("title",title);
                    context.startActivity(intent);

                }
            });
        }
    }
    // 榜单项ViewHolder
    public class ItemViewHolder extends RecyclerView.ViewHolder{
        private TextView tvItemTotalTitle;
        private  RecyclerView recyclerViewSecond;

        public ItemViewHolder(View itemView) {
            super(itemView);
            recyclerViewSecond = (RecyclerView) itemView.findViewById(R.id.recycler_frg_ranking_second_id);
            tvItemTotalTitle = ((TextView) itemView.findViewById(R.id.tv_frg_ranking_item_name));


        }


    }
}
